//
//  DSHTTPClient.m
//  DS08
//
//  Created by Ricky on 15/8/3.
//  Copyright (c) 2015年 Ricky. All rights reserved.
//

#import "DSHTTPClient.h"

@implementation DSHTTPClient

+ (instancetype)shareInstance{
    
    static DSHTTPClient *client = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        NSURL *url = [NSURL URLWithString:@"http://iosapi.itcast.cn/loveBeen/"];
        
        client = [[DSHTTPClient alloc]initWithBaseURL:url];
        client.requestSerializer = [[AFJSONRequestSerializer alloc]init];
    
        client.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    });
    return client;
}

+ (void)getUrlString:(NSString *)url
           withParam:(NSDictionary *)param
    withSuccessBlock:(successBlock)success
     withFailedBlock:(failedBlock)failed
      withErrorBlock:(errorBlock)error{
    
    [[self shareInstance] GET:url parameters:param progress:nil success:^(NSURLSessionDataTask *task, NSDictionary  *responseObject) {
        
       success(responseObject);
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        failed(error);
    }];
}

+ (void)postUrlString:(NSString *)url
            withParam:(NSDictionary *)param
     withSuccessBlock:(successBlock)success
      withFailedBlock:(failedBlock)failed
       withErrorBlock:(errorBlock)error{
    
    [[self shareInstance] POST:url parameters:param progress:nil success:^(NSURLSessionDataTask *task, NSDictionary  *responseObject) {
        success(responseObject);
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        failed(error);
    }];
}

//- (NSDictionary *)params{
//    
//    if (!_params) {
//        
//        _params = @{@"1":@"focus.json.php",// 首页数据
//                    @"2":@"firstSell.json.php",// 新鲜热卖
//                    @"5":@"supermarket.json.php",//超市
//                    @"6":@"search.json.php",// 搜索最新关键词
//                    @"7":@"ad.json.php",// 程序启动广告
//                    @"8":@"promotion.json.php",//搜索返回数据
//                    @"9":@"MyCoupon.json.php",//我的优惠券
//                    @"10":@"SystemMessage.json.php",// 系统信息
//                    @"11":@"UserMessage.json.php",//我的消息
//                    @"12":@"MyAdress.json.php",//收货地址
//                    @"13":@"MyOrders.json.php"//我的订单信息
//                    };
//    }
//    return _params;
//}


- (void)getDatawithParam:(NSString *)call WithUrlString:(NSString *)url  withSuccessBlock:(successBlock)success
         withFailedBlock:(failedBlock)failed
          withErrorBlock:(errorBlock)error{
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    
    [param setValue:call forKey:@"call"];
    
    [DSHTTPClient postUrlString:url withParam:param withSuccessBlock:success withFailedBlock:failed withErrorBlock:error];
}



@end
